using System;
using System.Collections.Generic;
using System.IO;

namespace SpellCore
{
    internal class HardCodedPathProviderFactory : ISpellCheckEngineFactory
    {
        private static string _suppliedDictionariesDirectory;
        private static string _userDictionariesDirectory;
        private static string _pathToEngineRootDirectory;

        #region IProviderFactory Members

        public IEnumerable<ISpellCheckEngine> GetEnginesForWritingSystem(string writingSystemId)
        {
            List<ISpellCheckEngine> engines = new List<ISpellCheckEngine>();
            writingSystemId = writingSystemId.Replace('-', '_');
            string userDictionaryWritingSystemPath = Path.Combine(UserDictionaryBaseDirectory, writingSystemId);
            if (!Directory.Exists(userDictionaryWritingSystemPath))
            {
                Directory.CreateDirectory(userDictionaryWritingSystemPath);
            }

            string suppliedDictionaryWritingSystemPath = Path.Combine(SuppliedDictionaryBaseDirectory, writingSystemId);
            if (!Directory.Exists(suppliedDictionaryWritingSystemPath))
            {
                Directory.CreateDirectory(suppliedDictionaryWritingSystemPath);
            }
            //System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles)
            
            HunSpellWrapper hunSpellWrapper =
                    HunSpellWrapper.CreateWrapper(PathToEngineRootDirectory,
                                                  suppliedDictionaryWritingSystemPath,
                                                  writingSystemId);
            if (hunSpellWrapper != null)
            {
                engines.Add(hunSpellWrapper);
            }
            ASpellWrapper aspellWrapper =
                    ASpellWrapper.CreateWrapper(PathToEngineRootDirectory,
                                                suppliedDictionaryWritingSystemPath,
                                                userDictionaryWritingSystemPath,
                                                writingSystemId);
            if (aspellWrapper != null)
            {
                engines.Add(aspellWrapper);
            }

            AscensWrapper ascensWrapper =
            AscensWrapper.CreateWrapper(PathToEngineRootDirectory,
                                        suppliedDictionaryWritingSystemPath,
                                        userDictionaryWritingSystemPath,
                                        writingSystemId);
            if (ascensWrapper != null)
            {
                engines.Add(ascensWrapper);
            }

            return engines;
        }

        #endregion

        private static string AddSpellCoreNameToDirectory(string directory)
        {
            string directoryWithoutFinalPathSeparator = directory;
            char finalCharOfDirectory = directory[directory.Length-1];
            if(finalCharOfDirectory==Path.DirectorySeparatorChar ||
                finalCharOfDirectory == Path.AltDirectorySeparatorChar)
            {
                directoryWithoutFinalPathSeparator = Path.GetDirectoryName(Path.GetFullPath(directory));
            }
            if (!directoryWithoutFinalPathSeparator.EndsWith(Speller.SpellCoreName))
            {
                directory = Path.Combine(directory, Speller.SpellCoreName);
            }
            return directory;
        }

        /// <summary>
        /// The base directory where all the user spellcore related files exist.
        /// </summary>
        /// <remarks>SpellCore directory will be automatically appended to the path on setting
        /// </remarks>
        internal static string UserDictionaryBaseDirectory
        {
            get
            {
                if (String.IsNullOrEmpty(_userDictionariesDirectory))
                {
                    UserDictionaryBaseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                }
                return _userDictionariesDirectory;
            }
            set
          {
              _userDictionariesDirectory = AddSpellCoreNameToDirectory(value);
              if (!Directory.Exists(_userDictionariesDirectory))
                {
                    Directory.CreateDirectory(_userDictionariesDirectory);
                }
            }
        }

        /// <summary>
        /// The base directory where all the user spellcore related files exist.
        /// </summary>
        /// <remarks>SpellCore directory will be automatically appended to the path on setting
        /// </remarks>
        internal static string SuppliedDictionaryBaseDirectory
        {
            get
            {
                if (String.IsNullOrEmpty(_suppliedDictionariesDirectory))
                {
                    SuppliedDictionaryBaseDirectory=Speller.PathToSpellCoreRoot;
                }
                return _suppliedDictionariesDirectory;
            }
            set
            {
                _suppliedDictionariesDirectory = AddSpellCoreNameToDirectory(value);
                if (!Directory.Exists(_suppliedDictionariesDirectory))
                {
                    Directory.CreateDirectory(_suppliedDictionariesDirectory);
                }
            }
        }

        internal static string PathToEngineRootDirectory
        {
            get
            {
                if(string.IsNullOrEmpty(_pathToEngineRootDirectory))
                {
                    PathToEngineRootDirectory = Path.Combine(Speller.PathToSpellCoreRoot, "Engines");
                }
                return _pathToEngineRootDirectory;
                
            }
            set { _pathToEngineRootDirectory = value; }
        }
    }
}